/**
 * Copyright (C) 2011-20077
 * 设计：		Zhangsk
 * 创建时间：	2012-03-03
 * Flash视频会议核心库 音频控制器类
 *
 * 
 */

package com.treemon.meetingcore.session
{
	import com.treemon.meetingcore.event.MeetingAudioEvent;
	import com.treemon.meetingcore.event.MeetingEvent;
	
	import flash.utils.Dictionary; 

	public class AudioControl extends ControlBase
	{
		{
			trace("AudioControl");
		}
		
		public function AudioControl(aSession: MeetingSession)
		{
			super(aSession);
			
			
		}
		
		override protected function addListener(): void
		{
			_session.addEventListener(MeetingEvent.AudioAccept, handleAudioAccept);
			_session.addEventListener(MeetingEvent.AudioClose, handleAudioClose);			
		} 
		
		public function addSpeaker(aRTMPAddr: String): void
		{
			var nIndex: int = aRTMPAddr.lastIndexOf("/");
			var serverid: String = aRTMPAddr.substring(0, nIndex);
			var streamid: String = aRTMPAddr.substring(nIndex + 1);

			createAudio(serverid, streamid);
		}
				
		private function handleAudioAccept(aEvent: MeetingEvent): void 
		{
			/*
<iq from="575751@slavemcu_1.machine1.com" type="result" id="" to="575751@masterjadc2s_1.machine1.com" xmlns="jabber:client">
  <query xmlns="meeting:voice:accept">
    <id>575751@slavemcu_1.machine1.com/&lt;asd&gt;</id>
    <mmid>01000000-00000002</mmid>
    <rtmp>rtmp://192.168.1.101/live/gbt_media_575751_01000000-00000002_a</rtmp>
  </query>
</iq>			
			*/
			var xmlQuery: XML = aEvent.xml.children()[0];
			var ns: Namespace = xmlQuery.namespace();
			//var strMMType: String = xmlQuery.ns::description.mmtype;
			var strRTMP: String = xmlQuery.ns::rtmp;
			var strMMID: String = xmlQuery.ns::mmid;
			var strJID:String = xmlQuery.ns::id;

			var evtAudioAccept: MeetingAudioEvent = new MeetingAudioEvent(MeetingAudioEvent.AudioAccept);
			var nIndex: int = strRTMP.lastIndexOf("/");
			evtAudioAccept.serverid = strRTMP.substring(0, nIndex);
			evtAudioAccept.streamid = strRTMP.substring(nIndex + 1);
			evtAudioAccept.mmid = strMMID;
			//evtAudioAccept.jid = strJID;
			createAudio(evtAudioAccept.serverid, evtAudioAccept.streamid);
			_session.dispatchEvent(evtAudioAccept);
			_session._userManager.modifiedAudioState(strJID,true);

			/*
			//mg:creattransport
			
			var xmlQuery: XML = aEvent.xml.children()[0];
			var ns: Namespace = xmlQuery.namespace();
			var strMMType: String = xmlQuery.ns::description.mmtype;
			var strRTMP: String = xmlQuery.ns::description.rtmp;
			var strMMID: String = xmlQuery.ns::description.mmid;
			if (strMMType == "audio")
			{
				var evtAudioAccept: MeetingAudioEvent = new MeetingAudioEvent(MeetingAudioEvent.AudioAccept);
				var nIndex: int = strRTMP.lastIndexOf("/");
				evtAudioAccept.streamid = strRTMP.substring(0, nIndex);
				evtAudioAccept.serverid = strRTMP.substring(nIndex + 1);
				evtAudioAccept.mmid = strMMID;
				createAudio(evtAudioAccept.serverid, evtAudioAccept.streamid);
				_session.dispatchEvent(evtAudioAccept);
			}
			*/
		}
		
		private function handleAudioClose(aEvent: MeetingEvent): void
		{
			var xmlQuery: XML = aEvent.xml.children()[0];
			var ns: Namespace = xmlQuery.namespace();
			var strRTMP: String = xmlQuery.ns::rtmp;
			var strMMID: String = xmlQuery.ns::mmid;
			var strJID: String = xmlQuery.ns::id;
			
			var evtAudioEvent: MeetingAudioEvent = new MeetingAudioEvent(MeetingAudioEvent.AudioClose);
			var nIndex: int = strRTMP.lastIndexOf("/");
			evtAudioEvent.serverid = strRTMP.substring(0, nIndex);
			evtAudioEvent.streamid = strRTMP.substring(nIndex + 1);
			evtAudioEvent.mmid = strMMID;
			//evtAudioEvent.jid = strJID;
			closeAudio(evtAudioEvent.serverid, evtAudioEvent.streamid);
			_session.dispatchEvent(evtAudioEvent);
			_session._userManager.modifiedAudioState(strJID,false);
			
		}
		
		public function createAudio(serverid: String, streamid: String): Boolean
		{
			var bResult:Boolean = true;
			try
			{
				if(_iCount < _maxCount)
				{
					var audio: AudioEntity = new AudioEntity(serverid, streamid);
					//videoplay.InitPlay(serverid,streamid,_width,_height);
					//插入字典
					_dictAudio[serverid+streamid] = audio;
					//_iCount++；
				}
			}
			catch(e:Error)
			{
				trace("AudioControl.createAudio error:"+e.message+"|name:" +e.name+"|id:"  +e.errorID);    		
			}
			return bResult;
		}
		
		public function closeAudio(AServerID: String, ASreamID: String): Boolean
		{
			var bResult:Boolean = false;
			try
			{
				//var nCounts: int = _arr.length;
				var oAudio: AudioEntity;
				for (var strKey: String in _dictAudio)
				{
		    		oAudio = (AudioEntity)(_dictAudio[strKey]);
		    		//trace(oAudio.serverid);
		    		//trace(oAudio.streamid);
		    		if ((AServerID == oAudio.serverid) && (ASreamID == oAudio.streamid))
		    		{
		    			oAudio = null;
		    			//_arr[i]  del  zhangsk
		    			//oAudio.visible = false;
		    			//oVideoPlay = null;
		    			//this.removeChild(oVideoPlay);
		    			//strKey = null;
		    			delete _dictAudio[strKey];
		    			bResult = true;
		    			break;
		    		}
		    	}
			}
			catch(e:Error)
			{
				trace("VideoPanel.closeVideo error:"+e.message+"|name:" +e.name+"|id:"  +e.errorID);    		
	    	}
			return bResult;
		}
		
		private var _iCount: int = 0;
		private var _dictAudio: Dictionary = new Dictionary();
		private var _maxCount: int = 9;

	}
}